package com.neustudy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neustudy.base.exception.NeuStudyExecption;
import com.neustudy.content.mapper.TeachplanMapper;
import com.neustudy.content.mapper.TeachplanMediaMapper;
import com.neustudy.content.model.dto.SaveTeachplanDto;
import com.neustudy.content.model.dto.TeachplanDto;
import com.neustudy.content.model.po.Teachplan;
import com.neustudy.content.model.po.TeachplanMedia;
import com.neustudy.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author WKX
 * @version 1.0
 * @describe todo
 * @create: 2024-11-10 19:08
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired
    TeachplanMapper teachplanMapper;

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {

        Long id = teachplanDto.getId();
        Teachplan teachplan = new Teachplan();
        if(id != null){
            teachplan = teachplanMapper.selectById(id);
            if(teachplan == null){
                throw new NeuStudyExecption("无此教学计划，请选择正确计划！");
            }
            BeanUtils.copyProperties(teachplanDto , teachplan);
            int i = teachplanMapper.updateById(teachplan);
            if(i < 0){
                throw new NeuStudyExecption("更新计划失败");
            }
        }else {
            BeanUtils.copyProperties(teachplanDto , teachplan);
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            teachplan.setOrderby(count + 1);
            int i = teachplanMapper.insert(teachplan);
            if(i < 0){
                throw new NeuStudyExecption("新增计划失败");
            }
        }
    }

    @Override
    public void deletTeachplan(Long teachPlanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        if(teachplan.getParentid() == 0){
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teachplan::getParentid , teachPlanId);
            int count = teachplanMapper.selectCount(wrapper);
            if(count > 0){
                throw new NeuStudyExecption("课程计划信息还有子级信息，无法操作");
            }else {
                int i = teachplanMapper.deleteById(teachPlanId);
                if(i<0){
                    throw new NeuStudyExecption("删除计划失败");
                }
            }
        }else {
            LambdaQueryWrapper<TeachplanMedia> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TeachplanMedia::getTeachplanId,teachPlanId);
            int i = teachplanMediaMapper.delete(wrapper);
            if(i<0){
                throw new NeuStudyExecption("删除计划对应的资源失败");
            }
            int j = teachplanMapper.deleteById(teachPlanId);
            if(j<0){
                throw new NeuStudyExecption("删除计划失败");
            }
        }
    }

    @Override
    public void moveUpOrDownTeachplan(String move, Long teachPlanId) {
        Teachplan teachplan = teachplanMapper.selectById(teachPlanId);
        if(move.equals("moveup")){
            int orderby = teachplan.getOrderby();
            if(orderby == 1){
                throw new NeuStudyExecption("不可上移，已到达最顶端");
            }else if(orderby > 1){
                LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Teachplan::getCourseId , teachplan.getCourseId());
                wrapper.eq(Teachplan::getParentid , teachplan.getParentid());
                wrapper.eq(Teachplan::getOrderby , orderby-1);
                Teachplan teachplanup = teachplanMapper.selectOne(wrapper);
                teachplanup.setOrderby(orderby);
                int i = teachplanMapper.updateById(teachplanup);
                if(i < 0){
                    throw new NeuStudyExecption("上移失败");
                }
                teachplan.setOrderby(orderby-1);
                int j = teachplanMapper.updateById(teachplan);
                if(j < 0){
                    throw new NeuStudyExecption("上移失败");
                }
            }else {
                throw new NeuStudyExecption("数据库错误");
            }
        }else if(move.equals("movedown")) {
            int orderby = teachplan.getOrderby();
            LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Teachplan::getCourseId , teachplan.getCourseId());
            wrapper.eq(Teachplan::getParentid , teachplan.getParentid());
            wrapper.eq(Teachplan::getOrderby , orderby+1);
            Teachplan teachplandown = teachplanMapper.selectOne(wrapper);

            if(teachplandown == null){
                throw new NeuStudyExecption("不可下移，已到达最底端");
            }else {
                teachplandown.setOrderby(orderby);
                int i = teachplanMapper.updateById(teachplandown);
                if(i < 0){
                    throw new NeuStudyExecption("下移失败");
                }
                teachplan.setOrderby(orderby+1);
                int j = teachplanMapper.updateById(teachplan);
                if(j < 0){
                    throw new NeuStudyExecption("下移失败");
                }
            }
        }
    }


    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }

}
